Page({
	data: {
		urlAddress: getApp().globalData.imageUrl,
		scrollHeight: '',
		urlAddress1: '../../img/',
		goodsList: {
			num: 0
		},
		shopInfo: {},
		goodsType: '',
		isShowShop: false,
		showAnImg: false
	},
	onLoad: function (options) {

		// 生命周期函数--监听页面加载
		var _self = this;
		wx.getSystemInfo({
			success: function (res) {
				_self.setData({
					scrollHeight: (750 / res.windowWidth * res.windowHeight - 136 - 128) + 'rpx'
				})

			}
		})
	},
	onShow: function () {
		console.log("*****")
		console.log(getApp().globalData.common.reserveShopCode)
		console.log("++++")
		if (getApp().globalData.common.reserveShopCode) {

		} else {
			getApp().globalData.common.reserveShopCode = getApp().globalData.common.shopCode;
		}
		this.initShop();
	},
	initShop: function () {
		if (!getApp().globalData.common.reserveShopCode) {
			this.setData({
				isShowShop: false,
				noteMsg: '商家歇业',
				shopInfo: {
					shopName: '请选择门店'
				}
			})
			return;
		}
		var _self = this;
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/reserve/shop/init.do',
			data: {
				shopCode: getApp().globalData.common.reserveShopCode,
				type: 1
			},
			header: getApp().globalData.header,
			success: function (res) {
				wx.hideLoading()
				if (res.data.code == 200) {
					if (res.data.obj.status == 1) {
						_self.setData({
							isShowShop: true,
							noteMsg: '商品售罄'
						})
					} else {
						//getApp().globalData.common.reserveShopCode = null;
						_self.setData({
							isShowShop: false,
							noteMsg: '商家歇业'
						})
					}
					if (res.data.obj.categoryList == null) {
						res.data.obj.categoryList = [{
							reserveCart: [],
							categoryCode: []
						}]
						_self.setData({
							isShowShop: false
						})
					}

					for (var i = 0; i < res.data.obj.categoryList.length; i++) {
						if (i == 0) {
							res.data.obj.categoryList[i].bg_style = 'nomal';
						} else {
							res.data.obj.categoryList[i].bg_style = 'unnomal';
						}
					}
					console.log("*****")
					console.log(res.data.obj)
					var menuWidth;
					if (res.data.obj.categoryList.length <= 4) {
						menuWidth = 750 / res.data.obj.categoryList.length - 40;
					} else {
						menuWidth = 147.5;
					}
					var mentLeft;
					mentLeft = (menuWidth - 150) / 2;

					_self.setData({
						mentLeft: mentLeft + 'rpx',
						menuWidth: menuWidth + 'rpx',
						shopInfo: res.data.obj,
						goodsList: res.data.obj.categoryList[0].reserveCart,
						goodsType: res.data.obj.categoryList[0].categoryCode
					})
				} else {
					//console.log("***")
					//console.log(shopInfo)
					_self.setData({
						isShowShop: false,
						noteMsg: '商家歇业',
						shopInfo: {
							shopName: '请选择门店'
						}

					})

				}
			}
		})
	},
	getGoodsBytype: function (e) {
		var shopInfo = this.data.shopInfo;
		console.log(shopInfo.categoryList);
		for (var i = 0; i < shopInfo.categoryList.length; i++) {
			if (e.currentTarget.dataset.code == shopInfo.categoryList[i].categoryCode) {
				shopInfo.categoryList[i].bg_style = 'nomal';
			} else {
				shopInfo.categoryList[i].bg_style = 'unnomal';
			}
			this.setData({
				shopInfo: shopInfo,
				goodsType: e.currentTarget.dataset.code
			})
		}
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/reserve/goods.do',
			data: {
				shopCode: getApp().globalData.common.reserveShopCode,
				categoryCode: e.currentTarget.dataset.code,
				type: 1
			},
			header: getApp().globalData.header,
			success: function (res) {
				console.log(res)
				wx.hideLoading();
				if (res.data.code == 200) {
					_self.setData({
						goodsList: res.data.obj
					})
				} else {

				}
			}
		})

	},
	rotateAndScaleThenTranslate: function (top, left) {
		this.setData({
			ani: 'transition: transform 1500ms ease 0ms; transform: translate(' + top + 'px, ' + left + 'px) scale(0.1, 0.1) rotate(0deg); transform-origin: 50% 50% 0px;'
		})
	},
	animationAct: function (elem, img) {
		if (!img) {
			img = this.data.urlAddress + '/xiaomai.jpg'
		}
		this.setData({
			ani: '',
			img: '',
			showAnImg: false

		})
		var uid = '#id_' + elem;
		console.log(uid)
		var _self = this;

		var endTop;
		var endLeft;
		wx.createSelectorQuery().select('#endMarker').boundingClientRect(function (rect) {
			endTop = rect.top;
			endLeft = rect.left;
		}).exec()

		wx.createSelectorQuery().select(uid).boundingClientRect(function (rect) {

			_self.setData({
				showAnImg: true,
				topStyle: rect.top,
				leftStyle: rect.left,
				img: img
			})
			_self.rotateAndScaleThenTranslate(endLeft - rect.left, endTop - rect.top);
		}).exec()

	},
	addAction: function (e) {
		if (e.currentTarget.dataset.num == 1) {
			this.animationAct(e.currentTarget.dataset.code, e.currentTarget.dataset.img)
		}
		var _self = this;
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/reserve/cart/add.do',
			data: {
				gbk: e.currentTarget.dataset.code,
				shopCode: getApp().globalData.common.reserveShopCode,
				num: e.currentTarget.dataset.num,
				categoryCode: _self.data.goodsType,
				type: 1
			},
			header: getApp().globalData.header,
			success: function (res) {
				wx.hideLoading();
				console.log(res)
				_self.setData({
					goodsList: res.data.obj
				})

			}
		})


	},
	turnPage: function () {

		getApp().globalData.common.reserveVid = null;
		wx.navigateTo({
			url: '../reservePay/reservePay?type=1'
		})

	},
	startAnimation: function (e) {


	}

})